//
//  HJMainViewController.swift
//  Micro-blog Sina
//
//  Created by admin on 16/3/20.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit
import SVProgressHUD

class HJMainViewController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // 自定义tabbar,是只读的,不能直接使用 = 进行赋值,因此用KVC替换
        let newTabBar = HJMianTabBar()
        self.setValue(newTabBar, forKey: "tabBar")
        
        addChildViewControllers()
        
        // 准备闭包
        newTabBar.composeClosure = { [weak self] () -> Void in
            HJPrint(msg: "撰写按钮被点击了")
            if !HJUserAccountViewModel.sharedInstance.userLogin {
                SVProgressHUD.showErrorWithStatus("没有登录不能发微博")
                return
            }
//            let composeVC = HJComposeViewController()
            // 根据字符串来创建类, Swift中通过类型来创建对象需要添加 产品名称.类名
            let productName = NSBundle.mainBundle().infoDictionary?["CFBundleName"] as? String ?? ""
            let cls = NSClassFromString("\(productName).HJComposeViewController") as! UIViewController.Type
            let composeVC = cls.init()
            self?.presentViewController(UINavigationController(rootViewController: composeVC), animated: true, completion: nil)
        }
    }
    private func addChildViewControllers() {
        // 设置barBar的TintColor
        tabBar.tintColor = UIColor.orangeColor()
        let homeVC = HJHomeViewController()
        addChildViewController(homeVC, title : "首页", imageName : "tabbar_home")
        let messageVC = HJMessageViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        let discoverVC = HJDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        let profileVC = HJProfileViewController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
    }
    /**
     为tabBar添加子控制器
     
     - parameter controller: controller
     - parameter title:      title
     - parameter imageName:  imageName
     */
    private func addChildViewController(controller : UIViewController, title : String, imageName : String) {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        let profileNav = UINavigationController(rootViewController: controller)
        addChildViewController(profileNav)
        
        
    }

}
